
package org.walklink.agent.plugin.impl.link;

import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.AnnotationTypeMatcher;
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.matcher.ElementMatchers;
import org.springframework.web.bind.annotation.PostMapping;
import org.walklink.agent.plugin.IPlugin;
import org.walklink.agent.plugin.InterceptPoint;

import javax.xml.bind.Element;

/**
 * @author lidishan
 */
public class LinkPlugin implements IPlugin {

    @Override
    public String name() {
        return getClass().getSimpleName();
    }

    @Override
    public InterceptPoint[] buildInterceptPoint() {
        return new InterceptPoint[]{
                new InterceptPoint() {
                    @Override
                    public ElementMatcher<TypeDescription> buildTypesMatcher() {
                        // 1. 尝试拦截一个注解
                        return ElementMatchers.hasAnnotation(
                                new AnnotationTypeMatcher<>(ElementMatchers.isAnnotatedWith(PostMapping.class))
                        );

//                         return ElementMatchers.nameStartsWith("org.walklink.walklinkdemo");
//                       return ElementMatchers.isAnnotatedWith(ElementMatchers.nameMatches("PostMapping"));
//                        return ElementMatchers.hasAnnotation(new AnnotationTypeMatcher().matches(PostMapping.class)
                    }

                    @Override
                    public ElementMatcher<MethodDescription> buildMethodsMatcher() {
                        return ElementMatchers.isMethod()
                                .and(ElementMatchers.any())
                                .and(ElementMatchers.not(ElementMatchers.nameStartsWith("main")));
                    }
                }
        };
    }

    @Override
    public Class adviceClass() {
        return LinkAdvice.class;
    }

}